package array;

import java.util.Arrays;

class Solution {
    public int[] shortestToChar(String s, char c) {
        int[] res = new int[s.length()];
        // 双轮遍历  填充最大值用于回头的时候取到最小值 如果不填充在头部的时候最小值全是0
        Arrays.fill(res, s.length());
        int cur = -1;
        for(int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == c) {
                cur = i;
                res[i] = 0;
                continue;
            }

            if(cur != -1) {
                res[i] = i - cur;
            }
        }

        cur = -1;
        for(int i = s.length() - 1; i >= 0; i--) {
            if(s.charAt(i) == c) {
                cur = i;
                continue;
            }

            if(cur != -1) {
                res[i] = Math.min(res[i], cur - i);
            }
        }

        return res;
    }
}
